﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using HZ.Supervise.Model;

namespace HZ.Supervise.Web.Secondphase.ReportCompany
{
    public partial class CompanyOfMonthJY : BasePage
    {
        string strMosth = "";
        private bool hide = true;
        GroupByField groupField = GroupByField.CompanyID_PS | GroupByField.SortSource;
        string sortField = "PuchaseCountSum ASC";
        string append = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
                BindList();
            }
        }
        void BindList()
        {
            SuperviseQuery query = ViewState["query"] as SuperviseQuery;
            strMosth = query.Mid;
            int t = 0;
            HZ.Supervise.IBLL.IBll_CompanyOrderDetail ibcd = HZ.Supervise.BLLFactory.IBll_CompanyOrderDetail();

            StringBuilder sqlwhere = new StringBuilder();

            if (!string.IsNullOrEmpty(Function.FormatPlanTime("AddTime", strMosth)))
            {
                sqlwhere.AppendFormat(" AND " + Function.FormatPlanTime("AddTime", strMosth));
            }


            if (!string.IsNullOrEmpty(txtcompanyname.Text.Trim()))
            {
                sqlwhere.AppendFormat(" AND BuyName='{0}' ", txtcompanyname.Text.Trim());
            }
            sqlwhere.Append(append);
            DataTable dt = ibcd.GetCompanySC_sort(5, sqlwhere.ToString(),sortField, 0, 1, out t);

            gvwSortInfo.DataSource = dt;
            gvwSortInfo.DataBind();
            for (int j = 0; j < gvwSortInfo.Rows.Count; j++)
            {
                //名称相同才能合并
                //防止超出索引
                if (j < (gvwSortInfo.Rows.Count - 1))
                {
                    if (gvwSortInfo.Rows[j].Cells[0].Text.Trim() == gvwSortInfo.Rows[j + 1].Cells[0].Text.Trim())
                    {
                        //合并头
                        gvwSortInfo.Rows[j].Cells[0].RowSpan = 2;
                        gvwSortInfo.Rows[j + 1].Cells[0].Visible = false;

                        //合并尾
                        gvwSortInfo.Rows[j].Cells[4].RowSpan = 2;
                        gvwSortInfo.Rows[j + 1].Cells[4].Visible = false;
                        j++;
                    }
                    else
                    {

                    }
                }

                //合并

            }
        }

        protected void gvwSortInfo_RowCreated(object sender, GridViewRowEventArgs e)
        {
            #region 合并表头
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:

                    //取得下拉框的月份

                    //第一行表头
                    TableCellCollection tcHeader = e.Row.Cells;
                    tcHeader.Clear();
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[0].Attributes.Add("colspan", "1"); //跨Row
                    tcHeader[0].Attributes.Add("bgcolor", "LightSteelBlue");
                    //tcHeader[0].Attributes.Add("bgcolor", "white");
                    tcHeader[0].Text = "";
                    //tcHeader.Add(new TableHeaderCell());
                    //tcHeader[1].Attributes.Add("bgcolor", "Red");
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[1].Attributes.Add("colspan", "6"); //跨Column
                    tcHeader[1].Text = strMosth;

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[2].Attributes.Add("colspan", "1"); //跨Column
                    tcHeader[2].Text = "</th></tr><tr>";



                    //第2行表头
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[3].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[3].Text = "企业";

                    //tcHeader.Add(new TableHeaderCell());
                    //tcHeader[4].Attributes.Add("bgcolor", "Khaki");
                    //tcHeader[4].Text = "类别";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[4].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[4].Text = "是否基药";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[5].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[5].Text = "经营企业采购总数量";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[6].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[6].Text = "经营企业入库总数量";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[7].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[7].Text = "经营企业出库数量";
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[8].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[8].Text = "经营企业出库金额";
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[9].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[9].Text = "经营企业库存数量";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[10].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[10].Text = "操作</th></tr><tr>";
                    break;
            }
            #endregion
        }

        protected void gvwSortInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "CompanyCurrentGoodsListJY")
            {
                string sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = ViewState["query"] as SuperviseQuery;

                query.CompanyName_PS = e.CommandArgument.ToString();

                Session[sessionid] = query;

                if (!string.IsNullOrEmpty(query.CompanyName_PS))
                {
                    Response.Redirect(string.Format("CompanyCurrentGoodsListJY.aspx?sessionid={0}", sessionid));
                }
                else
                {

                }
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            if (!this.droSort.SelectedValue.Equals("-1"))
            {
                append = "AND Source = " + droSort.SelectedValue;
            }
            else
            {
                append = "";
            }
            sortField = this.droSortField.SelectedValue;
            AspNetPager1.CurrentPageIndex = 1;
            BindList();
        }

    }
}
